﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcMembership
{
	public class GlobalApplication : System.Web.HttpApplication
	{
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            //TODO:Atenção
            /*Local*/
            RegisterRoutes( RouteTable.Routes );
            
            /*Servidor: Papagaio*/
            //RegisterRoutes();
        }

        public void RegisterRoutes()
        {
            RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            RouteTable.Routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

            RouteTable.Routes.Add(
              new Route("home.aspx",
                GenRouteDict("controller", "Home", "action", "Index"),
                new MvcRouteHandler())
            );

            RouteTable.Routes.Add(
              new Route("home.aspx/{page}",
                GenRouteDict("controller", "Home", "action", "Page", "page", null),
                new MvcRouteHandler())
            );
        }

        /// <summary>Create a RouteDictionary instance using couples from an object list.</summary> 
        /// <remarks>Useful because the fancy Dictionary constructor syntax is not supported in .NET 2.0.</remarks> 
        public RouteValueDictionary GenRouteDict(params object[] s)
        {
            IDictionary<string, object> ret = new Dictionary<string, object>();

            for (int i = 0; (i + 1) < s.Length; i += 2)
            {
                string a = s[i].ToString();
                object b = s[i + 1];

                ret.Add(a, b);
            }

            return new RouteValueDictionary(ret);
        }
	}
}